/*
 * Copyright (C) 2009 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.collections;

import java.util.Collection;
import java.util.Map;
import java.util.HashMap;

/**
 * Utility functions for dealing with {@link java.util.Map}s
 */
public class Maps {
    private Maps() {}

    /**
     * Creates a new hash map from the given collection of pairs, where first element
     * represent keys and second elements represent values.
     * <p>
     * If there are more than one entry for some key, the latst value in the collection will be used.
     * @param pairs collection of pairs, not null
     * @param <E1> type of keys
     * @param <E2> type of values
     * @return a new map created from the given collection of tuples or empty map if {@code pairs} is empty.
     */
    public static <E1, E2> Map<E1, E2> newHashMap(Collection<Tuple2<E1, E2>> pairs) {
        Map<E1, E2> map = newHashMap();
        for(Tuple2<? extends E1, ? extends E2> pair : pairs) {
            map.put(pair.get1(), pair.get2());
        }
        return map;
    }

    /**
     * Creates a new hash map with the default parameters
     * @param <E1> type of keys
     * @param <E2> type of values
     * @return a new map
     */
    private static <E1, E2> HashMap<E1, E2> newHashMap() {
        return new HashMap<E1,E2>();
    }
}
